﻿@page "/add"
@using Microsoft.AspNetCore.Components
@using SPA.Data

<div class="layui-container fly-marginTop">
  <div class="fly-panel" pad20 style="padding-top: 5px;">
    @if (IsAdded)
    {
       <div style="padding: 100px 0; display: flex; align-items: center;justify-content: center; flex-direction: column">
             <i class="layui-icon layui-icon-ok-circle" style="color: #5FB878; font-size: 100px;"></i>
             <p>发布成功 ，<NavLink class="nav-link" href="@($"detail/{AddedId}")">点击查看</NavLink></p>
       </div>
    }
    else
    {
      <div class="layui-form layui-form-pane">
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li class="layui-this">发表新帖<!-- 编辑帖子 --></li>
        </ul>
        <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
          <div class="layui-tab-item layui-show">
            <form action="" method="post" @onsubmit="Submit">
              <div class="layui-row layui-col-space15 layui-form-item">
                <div class="layui-col-md12">
                  <label class="layui-form-label">昵称</label>
                  <div class="layui-input-block">
                    <input type="text" @bind="Author" required autocomplete="off" class="layui-input"/>
                  </div>
                </div>
              </div>
              <div class="layui-row layui-col-space15 layui-form-item">
                <div class="layui-col-md12">
                  <label class="layui-form-label">标题</label>
                  <div class="layui-input-block">
                    <input type="text" @bind="Title" required autocomplete="off" class="layui-input"/>
                  </div>
                </div>
              </div>
              <div class="layui-form-item layui-form-text">
                <div class="layui-input-block">
                  <textarea name="content" @bind="Content" required  placeholder="详细描述" class="layui-textarea fly-editor" style="height: 260px;"></textarea>
                </div>
              </div>
              <div class="layui-form-item">
                <button class="layui-btn" type="submit">
                @if (IsAdding)
                {
                  <i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>
                  @:发布中
                }
                else
                {
                  @:立即发布
                }
                 </button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    }
  </div>
</div>

@code {
  
  public bool IsAdded { get; set; }
  public long AddedId { get; set; }
  
  public string Title { get; set; }
  public string Author { get; set; }
  public string Content { get; set; }
  
  public bool IsAdding { get; set; }
  


  async Task Submit()
  {
    
    IsAdding = true;
    var thread = new Thread()
    {
      Author = Author,
      Content = Content,
      AddTime = DateTime.Now,
      Comment = 0,
      Title = Title,
      View = 0
    };
    using (var db = new DbClient())
    {
      AddedId =  await db.Insertable<Thread>(thread).ExecuteReturnBigIdentityAsync();
    }
    Author = "";
    Content = "";
    Title = "";
    IsAdding = false;
    IsAdded = true;
  }

}
